﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;

namespace LaunchPad.Core.Controls.Tracks
{
    /// <summary>
    /// Interaction logic for RangeSelectionControl.xaml
    /// </summary>
    public partial class CueControl : UserControl
    {
        private Point _clickPos;

        public CueControl()
        {
            InitializeComponent();
            MouseDown += CueControlMouseDown;
            MouseMove += CueControlMouseMove;
            MouseUp += CueControlMouseUp;
            KeyDown += CueControlKeyDown;
            GotFocus += CueControlGotFocus;
            LostFocus += CueControlLostFocus;
        }

        public double Position
        {
            get { return (double) GetValue(Canvas.LeftProperty); }

            set
            {
                SetValue(Canvas.LeftProperty, value);
                if (PositionChanged != null)
                    PositionChanged(this, EventArgs.Empty);
            }
        }

        public double LeadInLength
        {
            get { return leadInCol.Width.Value; }
            set
            {
                if (value != leadInCol.Width.Value)
                {
                    leadInCol.Width = new GridLength(value);
                }
            }
        }

        public double LeadOutLength
        {
            get { return leadOutCol.Width.Value; }
            set
            {
                const double minWidth = 9;
                double width = Math.Max(minWidth, value);
                if (width != leadOutCol.Width.Value)
                {
                    leadOutCol.Width = new GridLength(width);
                }
            }
        }

        public event EventHandler PositionChanged = delegate { };
        public event EventHandler MoveComplete = delegate { };
        public event EventHandler MoveBeginning = delegate { };

        public void HideLeads()
        {
            LeadBorder.BorderBrush = LeadBorder.Background = new SolidColorBrush(Colors.Transparent);
        }

        private void CueControlLostFocus(object sender, RoutedEventArgs e)
        {
            OnMoveComplete();
        }

        private void CueControlGotFocus(object sender, RoutedEventArgs e)
        {
            OnMoveBeginning();
        }


        //void CueControl_MouseEnter(object sender, MouseEventArgs e)
        //{
        //    EffectTrackEvent ete = DataContext as EffectTrackEvent;
        //    ete.IsHovered = true;
        //}

        //void CueControl_MouseLeave(object sender, MouseEventArgs e)
        //{
        //    EffectTrackEvent ete = DataContext as EffectTrackEvent;
        //    ete.IsHovered = false;
        //}

        private void CueControlMouseUp(object sender, MouseButtonEventArgs e)
        {
            if (IsMouseCaptured)
                ReleaseMouseCapture();
            Opacity = 1;

            if (!IsFocused)
                OnMoveComplete();

            timePopup.IsOpen = false;

            e.Handled = true;
        }

        private void CueControlMouseMove(object sender, MouseEventArgs e)
        {
            if (IsMouseCaptured)
            {
                var mousePos = e.GetPosition(Parent as Canvas).X;
                Position = mousePos - _clickPos.X;
                ForcePopupUpdate();
                e.Handled = true;
            }
        }

        private void ForcePopupUpdate()
        {
            timePopup.HorizontalOffset = 0.1;
            timePopup.HorizontalOffset = 0;
        }

        private void CueControlMouseDown(object sender, MouseButtonEventArgs e)
        {
            Focus();

            if (e.LeftButton == MouseButtonState.Pressed)
            {
                _clickPos = e.GetPosition(this);

                CaptureMouse();
                Opacity = 0.5;
                OnMoveBeginning();

                timePopup.IsOpen = true;

                e.Handled = true;
            }
        }

        private void CueControlKeyDown(object sender, KeyEventArgs e)
        {
            double increment = Keyboard.Modifiers.HasFlag(ModifierKeys.Control) ? 1.1 : 3;

            switch (e.Key)
            {
                case Key.Left:
                    Position -= increment;
                    e.Handled = true;
                    break;
                case Key.Right:
                    Position += increment;
                    e.Handled = true;
                    break;
                case Key.Delete:
                    if (MessageBox.Show("Delete this event?", "LaunchPad", MessageBoxButton.OKCancel) ==
                        MessageBoxResult.OK)
                    {
                        var ete = DataContext as EffectTrackEvent;
                        ete.Effect.Timeline.Remove(ete);
                    }
                    e.Handled = true;
                    break;
            }
        }

        public void UpdateBindings()
        {
            BindingExpression topExpression = GetBindingExpression(Canvas.TopProperty);
            if (topExpression != null)
                topExpression.UpdateTarget();

            BindingExpression leftExpression = GetBindingExpression(Canvas.LeftProperty);
            if (leftExpression != null)
                leftExpression.UpdateTarget();
        }

        private void OnMoveBeginning()
        {
            if (MoveBeginning != null)
                MoveBeginning(this, EventArgs.Empty);
        }

        private void OnMoveComplete()
        {
            if (MoveComplete != null)
                MoveComplete(this, EventArgs.Empty);
        }
    }
}